/**
 * 
 */
package data;

import java.util.ArrayList;

import sharedData.IDataItem;

/**
 * Synchronized collection for holding IDataItem objects.
 * @author Didzis Petkus, Danny Johansen, Liviu Berheci
 * 
 */
public class DataQueue implements IDataQueue {

	private ArrayList<IDataItem> innerList;

	public DataQueue() {
		this.innerList = new ArrayList<>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see BE.IDataQueue#add(sharedData.IDataItem)
	 */
	@Override
	public void add(IDataItem item) {
		if (item == null)
			return;

		synchronized (innerList) {
			innerList.add(item);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see BE.IDataQueue#addFirst(sharedData.IDataItem)
	 */
	@Override
	public void addFirst(IDataItem item) {
		if (item == null)
			return;

		synchronized (innerList) {
			innerList.add(0, item);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see BE.IDataQueue#poll()
	 */
	@Override
	public IDataItem poll() {
		synchronized (innerList) {
			if (innerList.isEmpty())
				return null;

			return innerList.remove(0);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see BE.IDataQueue#peek()
	 */
	@Override
	public IDataItem peek() {
		synchronized (innerList) {
			if (innerList.isEmpty())
				return null;

			return innerList.get(0);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see BE.IDataQueue#getSize()
	 */
	@Override
	public int getSize() {
		synchronized (innerList) {
			return innerList.size();
		}
	}

}
